package com.laotou99.wmsjjj.wmsjjj.api;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.laotou99.wmsjjj.wmsjjj.WmsjjjApplication;
import com.laotou99.wmsjjj.wmsjjj.constant.C;
import com.laotou99.wmsjjj.wmsjjj.service.BaseInfoService;
import com.laotou99.wmsjjj.wmsjjj.service.LOCService;

@RestController
@RequestMapping("/api/baseInfo")
public class BaseInfoController {
	
	@Autowired
	private BaseInfoService baseInfoService;
	
	//@Autowired
	//private LOCService locService;
	
	@Autowired
	private ApplicationContext content;

	@RequestMapping(value="/test1",method=RequestMethod.GET)
	public String test1(HttpServletRequest request) {
		System.out.println("BaseInfoController test1");
		
		baseInfoService.test3();
		Object bean = content.getBean("locService");
		System.out.println("bean==null");
		System.out.println(bean==null);

		Object bean1 = WmsjjjApplication.getBean("locService");
		System.out.println("bean1==null");
		System.out.println(bean1==null);
		return "OK";
	}
	
	@RequestMapping(value="/serv_meth/{servAndMeth}/{method1}",method=RequestMethod.POST)
	public String serviceMethod(@PathVariable String servAndMeth,@PathVariable String method1,HttpServletRequest request) 
			throws NoSuchMethodException, SecurityException, IllegalAccessException, 
	IllegalArgumentException, InvocationTargetException, Exception
	{
		System.out.println("BaseInfoController serviceMethod");
		Object result = "";
		String serviceName = "";
		String methodName = "";

		Map map1 = request.getParameterMap();
		System.out.println("map1-=>"+JSON.toJSONString(map1));
		JSONObject jo1a = new JSONObject(map1.size());
		for (Object sss1 : map1.keySet()) {
			System.out.println("sss1-=>"+JSON.toJSONString(sss1));			
			//jo1.put(""+sss1, map1.get(sss1));
			Object[] obj2 = (Object[]) map1.get(sss1);
			System.out.println(""+obj2[0]);
			String tmp1 = ""+obj2[0];
			jo1a.put(""+sss1, tmp1);
		}
		servAndMeth = servAndMeth + "." + method1;
		System.out.println("servAndMeth-=>"+servAndMeth);
		System.out.println(StringUtils.lastIndexOf(servAndMeth, "."));
		try {
			if (C.isEmpty(servAndMeth) || StringUtils.lastIndexOf(servAndMeth, ".") == StringUtils.INDEX_NOT_FOUND) {
				throw new Exception("1,请求路由规则参数缺失或路由规则不合法，请检查。" );
			}
			String[] routerArr = StringUtils.split(servAndMeth,".");
			if (routerArr.length != 2) {
				throw new Exception("2,请求路由规则参数缺失或路由规则不合法，请检查。");
			}
			serviceName = routerArr[0];
			Object object = WmsjjjApplication.getBean(serviceName);
			Class c = object.getClass();
			methodName = routerArr[1];
			Method method = c.getMethod(methodName, JSONObject.class);
			result = method.invoke(object, jo1a);
		} catch (Exception e) {
			throw new Exception("系统繁忙，请稍后再试。",e);
		} 
		
		return ""+result;
	}

	@RequestMapping(value="/cmn/{serName}",method=RequestMethod.POST)
	public String common(@PathVariable String serName,HttpServletRequest request)
		throws NoSuchMethodException, SecurityException, IllegalAccessException, 
		IllegalArgumentException, InvocationTargetException 
	{
		
		System.out.println("BaseInfoController cmn");
		System.out.println("serName-=>"+serName);
		
//		Map<String, String[]> map1 = request.getParameterMap();
		Map map1 = request.getParameterMap();
		System.out.println("map1-=>"+JSON.toJSONString(map1));
		JSONObject jo1a = new JSONObject(map1.size());
		JSONObject jo1 = new JSONObject(map1);
		for (Object sss1 : map1.keySet()) {
			System.out.println("sss1-=>"+JSON.toJSONString(sss1));			
			//jo1.put(""+sss1, map1.get(sss1));
			Object[] obj2 = (Object[]) map1.get(sss1);
			System.out.println(""+obj2[0]);
			String tmp1 = ""+obj2[0];
			jo1a.put(""+sss1, tmp1);
		}
		for (Object obj1 : map1.entrySet()) {
			System.out.println("obj1-=>"+JSON.toJSONString(obj1));
		}
		
		System.out.println("map1-=>"+JSON.toJSONString(map1));
		System.out.println("jo1-=>"+JSON.toJSONString(jo1));
		System.out.println("jo1a-=>"+JSON.toJSONString(jo1a));
		Class<? extends BaseInfoService> c = baseInfoService.getClass();
		Method m1 = c.getMethod(serName, JSONObject.class);
		Object result = null;
		
		result = m1.invoke(baseInfoService, jo1a);
		
		//return "{\"aaa\":1234}";
		return ""+result;
	}
/*
map1-=>{"storeKey":["efee"],"storeName":["eefsds"],"storeNO":["afcdfa"],"address":["bbb"]}
map1-=>{"storeKey":["efee"],"storeName":["eefsds"],"storeNO":["afcdfa"],"address":["bbb"]}
 * */	
}
